/*********************************************************************************
 *      Copyright:  (C) 2021 Simply myself<1239726156@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  my_ds18b20.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2021年08月07日)
 *         Author:  Simply myself <1239726156@qq.com>
 *      ChangeLog:  1, Release initial version on "2021年08月07日 18时22分01秒"
 *                 
 ********************************************************************************/

#include "my_ds18b20.h"

/*
 * 获取DS18B20测量的温度信息
 */
int Get_DS18B20_Temp(float *temp)
{
	int              fd = -1;
	int              rv = -1;
	char             path[50] = "/sys/bus/w1/devices/";
	char             dir_name[20];
	int              found = -1;
	DIR             *dirp;
	struct dirent   *direntp;
	char             buf[128];
	char            *ptr;
	
	if ( !temp )
	{
		printf("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return -1;
	}
	
	//清空dir_name和buf内存空间的值，避免随机值产生乱码
	memset(dir_name, 0, sizeof(dir_name));
	memset(buf, 0, sizeof(buf));

	//打开文件夹"/sys/bus/w1/devices/"
	if ((dirp = opendir(path)) == NULL)
	{
		printf("[ERROR]: Open Directory '%s' Failure: %s\n", path, strerror(errno));
		return -1;
	}
#ifdef DEBUG
	printf("Open Directory '%s' Successfully\n", path);
#endif

	//在文件夹中找DS18B20的文件夹28-xx
	while ((direntp = readdir(dirp)) != NULL)
	{
		if (strstr(direntp->d_name, "28-") == NULL)
			continue;
		
		strncpy(dir_name, direntp->d_name, strlen(direntp->d_name));
#ifdef DEBUG
		printf("Find file: %s\n", dir_name);
#endif
		found = 1;
		break;
	}
	closedir(dirp);
	
	//找不到该文件夹
	if (found == -1)
	{
		printf("[ERROR]: Can not find DS18B20 in %s\n", path);
		return -2;
	}
	
	//将文件夹名和文件名分别拼接到path上	
	strncat(path, dir_name, strlen(dir_name));
	strncat(path, "/w1_slave", sizeof(path)-strlen(path));
	
	//打开w1_slave文件
	if ((fd = open(path, O_RDONLY)) < 0)
	{
		printf("[ERROR]: Open file '%s' Failure: %s\n", path, strerror(errno));
		return -3;
	}
#ifdef DEBUG
	printf("Open file '%s' fd[%d] Successfully\n", path, fd);
#endif

	//从w1_slave中读取内容
	if ((rv = read(fd, buf, sizeof(buf))) < 0)
	{
		printf("[ERROR]: Read data from file '%s' Failure: %s\n", path, strerror(errno));
		rv = -4;
		goto cleanup;
	}
#ifdef DEBUG
	printf("Read %dB data from file '%s'\n", rv, path);
#endif
	
	//从buf里匹配"t="，并将ptr移到数据的首地址上
	if ((ptr = strstr(buf, "t=") + 2) == NULL)
	{
		printf("[ERROR]: Find data Failure: %s", strerror(errno));
		rv = -5;
		goto cleanup;
	}
	
	//将数据转为double型，除1000得到正常的十进制温度
	*temp = atof(ptr)/1000;

cleanup:
	if (fd > 0)
		close(fd);

	//printf("Temperature reading completed\n");
	return rv;
}

